<?php namespace Admin\Controller;
class GoodsListController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\GoodsList;
	}
	public function index(){
		$gid=Q('get.gid',0,'intval');
		p($gid);
		$GoodAttrModel=new \Common\Model\GoodsAttr();
		$data=$GoodAttrModel
					 ->join("type_attr",'type_attr_taid','=',"taid")
					 ->where("goods_gid={$gid} AND type_attr.class=1")
					 ->groupBy('type_attr.taid')
					 ->get();
		foreach ($data as $k => $v) {
			$data[$k]['value']=$GoodAttrModel->where("goods_gid={$gid} AND type_attr_taid={$v['taid']}")->get();
		}
		
		
		//显示数据库里的信息
		
        //显示以后货品
        $goodsListModel = new \Common\Model\GoodsList();
        $listData = $goodsListModel->where("goods_gid={$gid}")->get();
        foreach ($listData as $k => $v) {
        	$vv=explode(',', $v['combo']);
        	foreach ($vv as $value) {
            $listData[$k]['value'][] = $GoodAttrModel->where("gtid ={$value}")->find();
            }
        }
       
        // p($listData);
        View::with('listData',$listData);

		View::with('data',$data);
		View::make();
	}
	public function add(){
		$GoodAttrModel=new \Common\Model\GoodsAttr();
		if(IS_POST){
			$glid=$this->model->store();
			if($glid){
				$data=Q("post.");
				$combo=implode(',',Q('post.combo'));
				$data['combo']=$combo;
    			 $data['value'][] = $GoodAttrModel->where("gtid IN({$data['combo']})")->get();
				$data['glid']=$glid;

       			//[sss=>"11"]  12231  {sss:"11"}
				echo json_encode($data);exit;
			}
		}
	}
	public function edit(){
		if(IS_POST){
			if($this->model->edit()){
				View::success("修改成功");
			}
		}
		$GoodAttrModel=new \Common\Model\GoodsAttr();
		// 抓取旧数据
		$glid=Q("get.glid");
		$oldData=$this->model->where("glid={$glid}")->find();
		// p($oldData['combo']);
		p($oldData);
		$oldData['combo']=explode(',',$oldData['combo']);
    	$data=$GoodAttrModel
					 ->join("type_attr",'type_attr_taid','=',"taid")
					 ->where("goods_gid={$oldData['goods_gid']} AND type_attr.class=1")
					 ->groupBy('type_attr.taid')
					 ->get();
		foreach ($data as $k => $v) {
			$data[$k]['value']=$GoodAttrModel->where("goods_gid={$oldData['goods_gid']} AND type_attr_taid={$v['taid']}")->get();
		}
		p($data);
		p($oldData);
		View::with('data',$data);
		View::with('oldData',$oldData);
		View::make();

	}
	public function del(){
		$glid=Q("get.glid",0,'intval');
		$this->model->where("glid={$glid}")->delete();
		View::success("删除成功");
	}
	
		
}
	